// pages/order/order.js
import {orderList} from "../../../api/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList:[],
    stateList:["全部","处理中","已确认","拒绝"],
    curIndex:0,
    textColor:""
  },
  //获取全部商品
async getOrderList(){
  let res = await orderList()
  console.log("订单",res)
  res.dataset.forEach(item => {
    item.year=item.realOrderTime.substr(0,4)
    item.yue=item.realOrderTime.substr(5,5)
    if(item.status == "已拒绝"){
      item.textColor="color1"
    }else if(item.status == "已处理"){
      item.textColor="color2"
    }else{
      item.textColor="color3"
    }
  });
  this.setData({
    orderList:res.dataset
  })
  console.log("date",this.data.orderList)
},
//点击导航
async changIndex(event){
  //console.log(event)
  let index = event.currentTarget.dataset.index
  this.setData({
    curIndex:index
  })
  if(index == 0){
    this.getOrderList()
  }else{
    let res = await orderList({
      status:index-1
    })
    res.dataset.forEach(item => {
      item.year=item.realOrderTime.substr(0,4)
       item.yue=item.realOrderTime.substr(5,5) 
       if(item.status == "已拒绝"){
        item.textColor="color1"
      }else if(item.status == "已处理"){
        item.textColor="color2"
      }else{
        item.textColor="color3"
      }
    });
    this.setData({
      orderList:res.dataset
    })
  }
},
//点击到搜索订单页
goSearchOrder(){
  wx.navigateTo({
    url: 'searchOrder',
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
 async onLoad(options) {
    

    //console.log("options",options)
    //console.log("options",options.branchId)

    if(JSON.stringify(options) != "{}"){
      if(options.branchId==undefined){
          //调取搜索接口
        let res = await orderList({
          status:Number(options.status),
          name:options.name
        })
        console.log("搜索",res)
        res.dataset.forEach(item => {
          item.year=item.realOrderTime.substr(0,4)
          item.yue=item.realOrderTime.substr(5,5) 
          if(item.status == "已拒绝"){
            item.textColor="color1"
          }else if(item.status == "已处理"){
            item.textColor="color2"
          }else{
            item.textColor="color3"
          }
        });
        this.setData({
          orderList:res.dataset
        })
      }else{
        //调取分店订单信息
        let res = await orderList({
          branchId:Number(options.branchId)
        })
        //console.log("fendian",res)
        res.dataset.forEach(item => {
          item.year=item.realOrderTime.substr(0,4)
          item.yue=item.realOrderTime.substr(5,5) 
          if(item.status == "已拒绝"){
            item.textColor="color1"
          }else if(item.status == "已处理"){
            item.textColor="color2"
          }else{
            item.textColor="color3"
          }
        });
        this.setData({
          orderList:res.dataset
        })
      }
      
    }else{
      this.getOrderList()
    }
  },
  //点击到订单详情
  godetial(event){
   // console.log("orderid",event.currentTarget.dataset.orderid)
    wx.navigateTo({
      url: `orderDetial?orderId=${event.currentTarget.dataset.orderid}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})